<?php


namespace app\admin\controller;
use app\admin\model\Extension as ExtensionModel;
use app\admin\model\WorksChapter as WorksChapterModel;
use app\admin\model\Works as WorksModel;
use app\admin\model\Order as OrderModel;

// 统计模块
class Statistics extends Base
{

    /**
     * 充值统计
     * date
     */
    public function recharge_statistics() {
        return $this->fetch();
    }
    /**
     * 短剧排行
     * date
     */
    public function short_play_ranking() {
        if (IS_POST) {
            $search = request()->param();
            $where = [];
            $where[] = ['w.is_del','=',0];
            $db   = WorksModel::alias('w')
                ->field('w.id,w.title,wc.channel_name,wt.theme_name,wl.name as level_name')
                ->join('works_channel wc','wc.id=w.wc_id')
                ->join('works_theme wt','wt.id=w.wt_id')
                ->join('works_level wl','wl.id=w.wl_id')
                ->where($where)->order('w.id desc');
            $list = $db->limit(paginator())->select();
            foreach ($list as &$item){
                $item['type'] = $item['channel_name'].' | '.$item['theme_name'];
                $item['ranking'] = '';
                $item['price'] = OrderModel::where('w_id',$item['id'])->sum('payment');
            }
            admin_response(0, 'success', $list, $db->count());
        }

        return $this->render_table([
            "page" => true,
            "cols" => [
//                ["type" => "checkbox"],
                ["field" => "ranking", "title" => "排行", "align" => "center", "sort" => true],
                ["field" => "title", "title" => "短剧名称", "align" => "center", "sort" => true],
                ["field" => "type", "title" => "数据分类", "align" => "center", "sort" => true],
                ["field" => "id", "title" => "书籍ID", "align" => "center", "sort" => true],
                ["field" => "price", "title" => "充值金额", "align" => "center", "sort" => true],
            ],

        ], false);
    }
    /**
     * 用户LTV
     * date
     */
    public function user_ltv() {
        if (IS_POST) {
            $weekArr = custom_time();
            $list = [];
            foreach ($weekArr as $key=>$vo) {
                $list[$key]['date'] = $vo['value'];
                $list[$key]['new_user'] = 0;
                $list[$key]['new_fans'] = 0;
                $list[$key]['rechargers_num'] = 0;
                $list[$key]['rechargers_price'] = '0.00';
                $list[$key]['price'] = '0.00';
            }
            admin_response(0, 'success', $list, count($list));
        }

        return $this->render_table([
            "page" => true,
            "cols" => [
//                ["type" => "checkbox"],
                ["field" => "date", "title" => "日期", "align" => "center"],
                ["field" => "new_user", "title" => "新增用户", "align" => "center"],
                ["field" => "new_fans", "title" => "新增粉丝", "align" => "center"],
                ["field" => "rechargers_num", "title" => "累计充值人数", "align" => "center"],
                ["field" => "rechargers_price", "title" => "累计充值金额", "align" => "center"],
                ["field" => "price", "title" => "当天充值", "align" => "center"],
            ],

        ], false);
    }
    /**
     * 全量、新增用户复充率
     * date
     */
    public function user_recharge_repeatedly() {
        if (IS_POST) {
            $weekArr = custom_time();
            $list = [];
            foreach ($weekArr as $key=>$vo) {
                $list[$key]['date'] = $vo['value'];
                $list[$key]['new_user'] = 0;
                $list[$key]['new_fans'] = 0;
                $list[$key]['rechargers_num'] = 0;
                $list[$key]['rechargers_price'] = '0.00';
                $list[$key]['price'] = '0.00';
            }
            admin_response(0, 'success', $list, count($list));
        }
        return $this->fetch();
    }
}